﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace OnlineMusic.lrc
{
    class GeciLrc : LrcBase
    {
        private string searchUrl = "http://geci.me/api/lyric/";
        public override string FindLrc(string title, string singer)
        {
            string url = searchUrl + Uri.EscapeUriString(title) + "/" + Uri.EscapeUriString(singer);
            string lrc = find(url);

            if (lrc == null)
            {
                url = searchUrl + Uri.EscapeUriString(title);
                lrc = find(url);
            }

            return lrc;
        }

        private string find(string url)
        {
            string result = wget(url);
            JObject json = JObject.Parse(result);
            JArray list = (JArray)json["result"];

            if (list.Count == 0)
            {
                return null;
            }

            return (string)list[0]["lrc"];
        }
    }
}
